// pages/editor_addres/editor_addres.js
var t = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:'',
    phone:'',
    addres:'',
    state:0,
    index:0,
    memberId:""
  },
  name:function(e){this.setData({name:e.detail.value})},
  phone: function (e) { this.setData({ phone: e.detail.value }) },
  addres: function (e) { this.setData({ addres: e.detail.value }) },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      state:options.state,
      index:options.index,
      memberId: wx.getStorageSync('memberInfo').id
    })
  
    if (options.state ==1){
      var list = wx.getStorageSync('addresList')
      console.log(options.index)
          this.setData({
            name:list[options.index].name,
            phone: list[options.index].phone,
            addres: list[options.index].addres,
          })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  formSubmit: function (t) {
    console.log(t)
    console.log("form发生了submit事件，携带数据为：", t.detail.value), this.overbooking(t.detail.value);
  },

  overbooking: function (o) {
    var a = this;
    wx.request({
      url: t.globalData.appletUrl + "/applet/saveAddress",
      data: o,
      header: {
        "content-type": "application/json"
      },
      success: function (t) {
        console.log(t.data)
          // "000000" == t.data.code ? a.openSuccess(a.data.shopId) :
        if ("000000" == t.data.code){
            wx.showToast({
              title: '保存成功',
              icon: "none",
              duration: 2e3
            });
          }
            
      }
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 保存
  save:function(){
    var myreg = /^[1][3,4,5,7,6,8,9][0-9]{9}$/;
    
    if(this.data.name == '' ||!myreg.test(this.data.phone) || this.data.addres == ''){
        wx.showToast({
          title: '请输入正确信息',
          icon:'none'
        })
        return
    }else{
      // wx.redirectTo({
      //   url: '../addres_list/addres_list?name='+this.data.name+'&phone='+this.data.phone+'&addres='+this.data.addres+'&state=0',
      // })
      var data = {}
      data['name'] = this.data.name,
      data['phone'] = this.data.phone,
      data['addres'] = this.data.addres
      data['state'] =  0
      var page = getCurrentPages()
      if(page.length >1){
            // 当前页面 
            var currPage = page[page.length-1]
            //上一页面
            var prevPage = page[page.length -2]
            prevPage.addres(data,this.data.state)
            wx.navigateBack({
              delta:1
            })
      }
    }

  }
})
